/**
 * 20220117:从原strips中分离
 * 20220713:correct the bug in [updateGeometry],witch caused by using [this.geometry]
 */
import HardLineGeometry from "./hard-line-geometry";
import {Uint8BufferAttribute} from "three";

/**
 * 多段线几何体
 * @param vertices {[Vector3]} 顶点序列
 * @param values {object} 参数对象
 *      {
 *          父类除箭头相关的参数
 *      }
 */
export default class StripsGeometry extends HardLineGeometry{
    constructor(vertices,values={}){
        super(vertices,values);
    }

    //...................................................public....................................................

    /**
     * 更新几何体
     */
    updateGeometry() {
        super.updateGeometry();
        this.getAttribute('count').copyArray(this._counts);
        this.getAttribute('count').needsUpdate = true;
    }

    //...................................................private....................................................

    _initProps(values) {
        super._initProps(values);
        this._props.remove('arrowMode','arrowLengthRat','arrowWidthRat');
    }
    _initVariable(){
        super._initVariable();
        let s=this._maxSegments;
        let s1=(2*s-3)*6;
        this._counts=new Float32Array(s1);
    }
    _setAttribute() {
        super._setAttribute();
        this.setAttribute('count',new Uint8BufferAttribute(this._counts,1));
    }

    _rectifyPoints(ps) {
        return ps;
    }
    _reRangeData(){
        let reRange=super._reRangeData();
        if(reRange){
            let s1=(2*this._maxSegments-3)*6;
            this._counts=new Float32Array(s1);
            this.setAttribute('count', new Uint8BufferAttribute(this._counts, 1));
        }
    }
    _getData(){
        this._rectifyVerts=this._rectifyPoints(this._verts);
        if(this._rectifyVerts.length<2){
            console.warn('At least 2 points needed!');
            return;
        }
        this._reRangeData();
        this._pushSolidData(this._rectifyVerts);
    }
    _pushData(i3,i1,c,n,o,ct){
        this._offsets[i1]=o;
        this._counts[i1]=ct;
        this._positions[i3]=c.x;
        this._positions[i3+1]=c.y;
        this._positions[i3+2]=c.z;
        this._nextPositions[i3]=n.x;
        this._nextPositions[i3+1]=n.y;
        this._nextPositions[i3+2]=n.z;
    }
    _pushSolidData(vs){
        let n0=this._tV0;
        let n1=this._tV1;
        for(let i=0;i<vs.length-1;i++){
            let cp=vs[i];
            let np=vs[i+1];
            let c0=i;
            let c1=i+1;
            if(i===0){
                n0.copy(np);
                n1.subVectors(np,cp).normalize().add(np);
                this._pushData(0,0,cp,n0,1,c0);
                this._pushData(3,1,cp,n0,-1,c0);
                this._pushData(6,2,np,n1,1,c1);
                this._pushData(9,3,cp,n0,-1,c0);
                this._pushData(12,4,np,n1,-1,c1);
                this._pushData(15,5,np,n1,1,c1);
            }else{
                let i3=18+(i-1)*36;
                let i1=6+(i-1)*12;
                n0.copy(np);
                n1.subVectors(np,cp).normalize().add(np);
                this._pushData(i3+18,i1+6,cp,n0,1,c0);
                this._pushData(i3+21,i1+7,cp,n0,-1,c0);
                this._pushData(i3+24,i1+8,np,n1,1,c1);
                this._pushData(i3+27,i1+9,cp,n0,-1,c0);
                this._pushData(i3+30,i1+10,np,n1,-1,c1);
                this._pushData(i3+33,i1+11,np,n1,1,c1);
            }
        }
    }
}